package com.muyi.controller.account;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletCaptcha", value = "/captcha")
public class ServletCaptcha extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用hutool工具包生成验证码
        GifCaptcha captcha = CaptchaUtil.createGifCaptcha(150, 45, 4);
        //获取验证码的值
        String code = captcha.getCode();
        //将验证码保存到sessioon
        request.getSession().setAttribute("code",code);
        //将验证码写回前端页面
        captcha.write(response.getOutputStream());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
